package com.db.williamchart.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.db.williamchart.ChartContract;
import com.db.williamchart.R;
import com.db.williamchart.animation.DefaultHorizontalAnimation;
import com.db.williamchart.animation.NoAnimation;
import com.db.williamchart.data.Frame;
import com.db.williamchart.data.Label;
import com.db.williamchart.data.Paddings;
import com.db.williamchart.data.configuration.BarChartConfiguration;
import com.db.williamchart.data.configuration.ChartConfiguration;
import com.db.williamchart.extensions.ExtensionUtils;
import com.db.williamchart.renderer.HorizontalBarChartRenderer;

import java.util.List;

public class HorizontalBarChartView extends AxisChartView implements ChartContract.BarView {

    private static final float defaultSpacing = 10f;
    private static final int defaultBarsColor = -0x1000000;
    private static final float defaultBarsRadius = 0F;

    private float spacing = defaultSpacing;

    @ColorInt
    private int barsColor = defaultBarsColor;

    private float barRadius = defaultBarsRadius;
    private int barsBackgroundColor = -1;

    public HorizontalBarChartView(@NonNull Context context) {
        this(context,null);
    }

    public HorizontalBarChartView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalBarChartView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        renderer = new HorizontalBarChartRenderer(this, painter, new NoAnimation());
        animation = new DefaultHorizontalAnimation();
        handleAttributes(ExtensionUtils.obtainStyledAttributes(this, attrs, R.styleable.BarChartAttrs));
        handleEditMode();
    }

    @Override
    public void drawLabels(List<Label> xLabels) {
        painter.prepare(labelsSize, labelsColor, labelsFont);
        labels.draw(canvas, painter.getPaint(), xLabels);
    }

    @Override
    public void drawGrid(Frame innerFrame, List<Float> xLabelsPositions, List<Float> yLabelsPositions) {
        grid.draw(canvas, innerFrame, xLabelsPositions, yLabelsPositions);
    }

    @Override
    public void drawDebugFrame(List<Frame> frames) {
        painter.prepare(-0x1000000, Paint.Style.STROKE);
        for (Frame frame : frames) {
            canvas.drawRect(frame.toRect(), painter.getPaint());
        }
    }

    @Override
    public void drawBars(List<Frame> frames) {
        painter.prepare(barsColor, Paint.Style.FILL);
        for (Frame frame : frames) {
            canvas.drawRoundRect(
                    frame.toRectF(),
                    barRadius,
                    barRadius,
                    painter.getPaint()
            );
        }
    }

    @Override
    public void drawBarsBackground(List<Frame> frames) {
        painter.prepare(barsBackgroundColor, Paint.Style.FILL);
        for (Frame frame : frames) {
            canvas.drawRoundRect(
                    frame.toRectF(),
                    barRadius,
                    barRadius,
                    painter.getPaint()
            );
        }
    }

    @Override
    ChartConfiguration getChartConfiguration() {
        return new BarChartConfiguration(
                getMeasuredWidth(),
                getMeasuredHeight(),
                new Paddings(
                        getPaddingLeft(),
                        getPaddingTop(),
                        getPaddingRight(),
                        getPaddingBottom()
                ),
                axis,
                labelsSize,
                scale,
                labelsFormatter,
                barsBackgroundColor,
                spacing
        );
    }

    private void handleAttributes(TypedArray typedArray) {
        if (typedArray != null) {
            spacing = typedArray.getDimension(R.styleable.BarChartAttrs_chart_spacing, spacing);
            barsColor = typedArray.getColor(R.styleable.BarChartAttrs_chart_barsColor, barsColor);
            barRadius = typedArray.getDimension(R.styleable.BarChartAttrs_chart_barsRadius, barRadius);
            barsBackgroundColor =
                    typedArray.getColor(R.styleable.BarChartAttrs_chart_barsBackgroundColor, barsBackgroundColor);
            typedArray.recycle();
        }
    }
}
